package xhwl.hmo.mall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.api.ApplyCardApi;
import xhwl.hmo.mall.http.api.ApplyCardStatusApi;
import xhwl.hmo.mall.http.bean.resp.AddressListResp;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.utils.SpUtil;
import xhwl.hmo.mall.utils.StringUtil;
import xhwl.hmo.mall.utils.ToastUtil;
import xhwl.hmo.mall.utils.XHLog;
import xhwl.hmo.mall.views.ProgressWebView;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by admin on 2017-11-06.
 */

public class CertificateActivity extends BaseActivity implements View.OnClickListener {
    public static final String EXTRA_BUT_ID="buyId";
    public static final String EXTRA_GOODS_ID="goodsId";
    public static final String EXTRA_SX_ID="sxid";
    public static final String EXTRA_PERSON_ID="personId";
    public static final String EXTRA_SALE_NAME="saleName";
    public static final String EXTRA_APPLY_CARD_FLAG="applyCardFlag";
    ImageView ivCertificate;
    private ProgressWebView webview;
    TextView tvBuy,tvApplyCard;
    String buyId;//申请实体卡使用
    boolean applyFlag;//是否需要显示申请实体卡按钮
    String certificateUrl="http://hmall.yk2020.com/index.php?m=Member&c=app&a=zs&sx_id=%s&person_id=%s&sale_name=%s";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buyId=getIntent().getStringExtra(EXTRA_BUT_ID);
        applyFlag=getIntent().getBooleanExtra(EXTRA_APPLY_CARD_FLAG,false);
        setContentView(R.layout.activity_certificate);
    }

    @Override
    public void initViews() {
        initTitleViews();
        initWebView();
        ivCertificate= (ImageView) findViewById(R.id.ivCertificate);
        tvApplyCard= (TextView) findViewById(R.id.tvApplyCard);
        tvBuy= (TextView) findViewById(R.id.tvBuy);
        if(!applyFlag){//第一次判断，来源不是商城的不能是申请实体卡
            tvApplyCard.setVisibility(View.GONE);
        }else{
            ApplyCardStatusApi api=new ApplyCardStatusApi(SpUtil.getString(SpUtil.MEMBER_MOBILE,""),buyId);
            HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp>() {
                @Override
                public void onNext(HttpResp resp) {
                    if(resp!=null&&resp.isOk()){
                        tvApplyCard.setVisibility(View.VISIBLE);
                    }
                    else {
                        tvApplyCard.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

    private void initWebView() {
        webview = (ProgressWebView) findViewById(R.id.webview);
        webview.getSettings().setDefaultTextEncodingName("UTF-8");
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    webview.progressbar.setVisibility(View.GONE);
                } else {
                    if (webview.progressbar.getVisibility() == View.GONE)
                        webview.progressbar.setVisibility(View.VISIBLE);
                    webview.progressbar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        this.titleTextView.setText("电子证书");
    }

    @Override
    public void initListener() {
        tvApplyCard.setOnClickListener(this);
        tvBuy.setOnClickListener(this);
    }

    @Override
    public void loadData() {
        Intent intent=getIntent();
        String goodsId=intent.getStringExtra(EXTRA_GOODS_ID);
        String saleName=intent.getStringExtra(EXTRA_SALE_NAME);
        String sxId=intent.getStringExtra(EXTRA_SX_ID);
        String personId=intent.getStringExtra(EXTRA_PERSON_ID);
        try {
            String url=String.format(certificateUrl,sxId,personId,URLEncoder.encode(saleName,"UTF-8"));
            XHLog.d("okhttp===url:"+url);
//            Glide.with(this).load(url).into(ivCertificate);
            webview.loadUrl(url);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBuy:
            {
                if(StringUtil.isEmpty(getIntent().getStringExtra(EXTRA_GOODS_ID))){
                    Intent intent = new Intent(mContext, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                    mContext.startActivity(intent);
                }else {
                    Intent intent = new Intent(mContext, SuiteProductsDetailActivity.class);
                    intent.putExtra("GOOD_ID", getIntent().getStringExtra(EXTRA_GOODS_ID));
                    mContext.startActivity(intent);
                }
            }
                break;
            case R.id.tvApplyCard:
            {
                Intent intent=new Intent(mContext,AddressManageActivity.class);
                startActivityForResult(intent,0);
            }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0&&resultCode==RESULT_OK){
            if(data!=null){
                AddressListResp.Address addr= (AddressListResp.Address) data.getSerializableExtra("data");
                applyCard(addr);
            }
        }
    }
    private void applyCard( AddressListResp.Address addr){
        ApplyCardApi applyCardApi=new ApplyCardApi(SpUtil.getString(SpUtil.MEMBER_MOBILE,""),buyId,addr.RECORD_ID);
        HttpManager.getInstance().dealHttp(this, applyCardApi, new OnNextListener<HttpResp>() {
            @Override
            public void onNext(HttpResp resp) {
                if(resp.isOk()){
                    tvApplyCard.setVisibility(View.GONE);
                    Intent intent=new Intent(mContext,ApplyCardSuccActivity.class);
                    startActivity(intent);
//                    finish();
                }
                else
                {
                    ToastUtil.showToast(mContext,resp.message);
                }
            }
        });
    }
}
